#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
const int N = 4e5 + 10;

int n;
vector<int> q;

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    int x;
    for(int i = 1; i <= n; i ++){
        cin >> x;
        q.push_back(x);
    }

    sort(q.begin(), q.end());
    int ck = 0;
    for(int i = 0; i < n; i ++){
        if(i != n - 1){
            if(q[i] == q[i + 1]){
                while(q[i] == q[i + 1] && i + 1 <= n - 1){
                    i ++;
                    ck ++;
                }
            }
        }
    }

    q.erase(unique(q.begin(), q.end()), q.end());
    for(int i = 1; i <= ck; i ++){
        q.push_back(1e9 + 10);
    }

    
    int tail = n - 1;
    int cur = 0;
    for(int i = 0; i <= tail;){

        if(q[i] == (cur + 1)) {
            cur ++;
            i++;    
        }else {
            if(tail - 1 >= i){
                tail -= 2;
                cur ++;
            }else{  
                cout << cur;
                exit(0);
            }
        }
    }

    cout << cur;


    return 0;
}